package com.glacier.framework.component.dto.response;

import com.glacier.framework.component.dto.DTO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.time.Instant;

/**
 * date 2023-12-21 13:44
 *
 * @author glacier
 * @version 1.0
 */
@Data
@ToString
@EqualsAndHashCode(callSuper = false)
public class Response<T> extends DTO {
    /**
     * 状态码
     */
    protected String code;
    /**
     * 状态信息
     */
    protected String message;
    /**
     * 数据
     */
    protected T data;

    /**
     * 时间戳
     */
    protected Long timestamp;

    public Response() {
        this(BaseResponse.OK.getCode(), null, null);
    }

    public Response(T data) {
        this(BaseResponse.OK.getCode(), null, data);
    }

    public Response(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = Instant.now().toEpochMilli();
    }

    public static <T> Response<T> success(T data) {
        return new Response<>(data);
    }

    public static <T> Response<T> error(String code, String message) {
        return new Response<>(code, message, null);
    }
}
